include Java
require 'gookup/common/usefull_extensions'
require 'gookup/system/settings_container'
require 'gookup/http/http_handler'
BLOGGER_OUTPUT=Settings[:output]+'/blogger_export.xml'
class Blogger2 
  include HttpHandler
  include_package('org.apache.commons.httpclient.methods')
  include_all(%w(client data util) , 'com.google.gdata')
  
  attr_reader :entries

  def initialize
    @service=GoogleService.new('blogger', 'gookup-gookupApp-1')
  end


  def login(user,pass)
    begin
      @service.setUserCredentials(user, pass)
      blogs_list_url = java.net.URL.new('http://www.blogger.com/feeds/default/blogs')
      id=@service.getFeed(blogs_list_url, com.google.gdata.data.Feed.java_class).entries.first.getId()
      @blogId=id.scan(/\d+/).last
      @auth=@service.authTokenFactory.authToken.value
    rescue ResourceNotFoundException => e
      raise Exception.new('no such blog name exists!')
    rescue Exception => e
      raise Exception.new('Failed to log in!')
    end
  end
  
  def fetch_all_entries
    get=GetMethod.new("http://www.blogger.com/feeds/#{@blogId}/archive")
    get.setRequestHeader("Authorization", "GoogleLogin auth=\"#{@auth}\"")
    @xml,code=call_http get
    @xml
  end
  
  def persist
    File.open(Settings[:output]+'/blogger_export.xml', 'w') do |file|
      file.puts @xml
    end
  end
  
end
